$(function () {

    $.fn.serializeObject = function()
    {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

    $(".btn-check").click(function(){
        if ($(this).is(":checked")){
            $(this).parent().find("#sprResult").val("0");
        }else{
            $(this).parent().find("#sprResult").val("1");
        }

        var data = $('#selfForm').serializeObject();
        $.ajax({
            type: 'post',
            url: '/self/selfCheck',
            datatype:"json",
            async:false,
            data: {data:JSON.stringify(data)},
            success: function () {
            },
            error: function (request) {
            }

        });
    });

    // 自检结果提交
    $("#btnSelfCheckReport").click(function () {
        var data = $('#selfForm').serializeObject();
        $.ajax({
            type: 'post',
            url: '/self/selfForm',
            datatype:"json",
            async:false,
            data: {data:JSON.stringify(data)},
            success: function () {
            },
            error: function (request) {
            }
        });
    });
});

// 自检图片加载
function selfImg(e){

    // 清除div中所有元素
    $("#picBox").empty();
    $("#listBox").empty();
    // 获取自检项目ID
    var smcId = e.name;
    var picHtml = "<ul class='cf'> ";
    var listHtml = "<ul class='cf'> ";
    // 根据自检项目ID获取图片LIST
    $.ajax({
        type: 'post',
        url: '/self/selfimg',
        data: {smcid: smcId},
        cache: false,
        dataType: 'json',
        success: function (data) {
            var imgList = data['selfimg'];
            for(var index in imgList){
                if(index != "unique"){
                    picHtml = picHtml + "<li> <a target='_blank'> <img width='680' height='380' src='"
                        + imgList[index].spimgAddress
                        + "' alt='' /> </a> </li>";
                    if(index < 1){
                        listHtml = listHtml + "<li class='on'><i class='arr2'></i><img width='120' height='64' src='"
                            + imgList[index].spimgAddress + "'/></li>";
                    }else{
                        listHtml = listHtml + "<li><i class='arr2'></i><img width='120' height='64' src='"
                            + imgList[index].spimgAddress + "'/></li>";
                    }
                }
            }
            picHtml = picHtml + "</ul>";
            listHtml = listHtml + "</ul>";
            $("#picBox").append(picHtml);
            $("#listBox").append(listHtml);

            // 用于实现图片轮播***************start******************
            function G(s){
                return document.getElementById(s);
            }

            function getStyle(obj, attr){
                if(obj.currentStyle){
                    return obj.currentStyle[attr];
                }else{
                    return getComputedStyle(obj, false)[attr];
                }
            }

            function Animate(obj, json){
                if(obj.timer){
                    clearInterval(obj.timer);
                }
                obj.timer = setInterval(function(){
                    for(var attr in json){
                        var iCur = parseInt(getStyle(obj, attr));
                        iCur = iCur ? iCur : 0;
                        var iSpeed = (json[attr] - iCur) / 5;
                        iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
                        obj.style[attr] = iCur + iSpeed + 'px';
                        if(iCur == json[attr]){
                            clearInterval(obj.timer);
                        }
                    }
                }, 30);
            }

            var oPic = G("picBox");
            var oList = G("listBox");

            var oPrev = G("prev");
            var oNext = G("next");
            var oPrevTop = G("prevTop");
            var oNextTop = G("nextTop");

            var oPicLi = oPic.getElementsByTagName("li");
            var oListLi = oList.getElementsByTagName("li");
            var len1 = oPicLi.length;
            var len2 = oListLi.length;

            var oPicUl = oPic.getElementsByTagName("ul")[0];
            var oListUl = oList.getElementsByTagName("ul")[0];
            var w1 = 680;
            var w2 = 129;

            oPicUl.style.width = w1 * len1 + "px";
            oListUl.style.width = w2 * len2 + "px";

            var index = 0;

            var num = 5;
            var num2 = Math.ceil(num / 2);

            function Change(){

                Animate(oPicUl, {left: - index * w1});

                if(index < num2){
                    Animate(oListUl, {left: 0});
                }else if(index + num2 <= len2){
                    Animate(oListUl, {left: - (index - num2 + 1) * w2});
                }else{
                    Animate(oListUl, {left: - (len2 - num) * w2});
                }

                for (var i = 0; i < len2; i++) {
                    oListLi[i].className = "";
                    if(i == index){
                        oListLi[i].className = "on";
                    }
                }
            }

            oNextTop.onclick = oNext.onclick = function(){
                index ++;
                index = index == len2 ? 0 : index;
                Change();
            }

            oPrevTop.onclick = oPrev.onclick = function(){
                index --;
                index = index == -1 ? len2 -1 : index;
                Change();
            }

            for (var i = 0; i < len2; i++) {
                oListLi[i].index = i;
                oListLi[i].onclick = function(){
                    index = this.index;
                    Change();
                }
            }
            // 用于实现图片轮播*************** end ******************

            // 弹出图片显示框
            if(imgList.length < 1){
                alert("该项目没有图片！");
            }else{
                $("#smallpic").css('display','block');
                $("#bigpic").css('display','block');
            }
        },
        error: function (request) {
            alert("提交失败");
        }
    });


}